package com.tanhua.recommend.listener;

import com.tanhua.domain.mongo.VideoScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author liuyp
 * @date 2021/02/23
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-video", consumerGroup = "tanhua-video-group")
public class VideoScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        log.info("====接收视频推荐消息【开始】====");

        // 1. 获取参数
        Long userId = Long.valueOf(map.get("userId").toString());
        Long vid = Long.valueOf(map.get("vid").toString());
        Integer type = (Integer) map.get("type");

        // 2. 根据操作类型进行计分
        VideoScore videoScore = new VideoScore();
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);
        videoScore.setDate(System.currentTimeMillis());

        switch (type) {
            case 1:
                videoScore.setScore(20d);
                break;
            case 2:
                videoScore.setScore(5d);
                break;
            case 3:
                videoScore.setScore(-5d);
                break;
            case 4:
                videoScore.setScore(10d);
                break;
            default:
        }

        // 3. 保存到MongoDB
        mongoTemplate.save(videoScore);
        log.info("====接收视频推荐消息【完成】====");
    }
}